package com.xian.ai.code.config;

import dev.langchain4j.community.store.memory.chat.redis.RedisChatMemoryStore;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @ClassName RedisChatMemoryStoreConfig
 * @Description Redis配置类，用于配置Langchain4j的Redis存储
 * @Author Zhangxn
 * @Version 1.0
 */
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis")
@Data
public class RedisChatMemoryStoreConfig {

    private String host;

    private int port;

    private String password;

    private long ttl;

    @Bean
    public RedisChatMemoryStore redisChatMemoryStore() {
        RedisChatMemoryStore.Builder builder = RedisChatMemoryStore.builder()
                .host(host)
                .user("default")
                .port(port)
                .ttl(ttl);
        
        // 只有当密码不为空且非空字符串时才设置密码
        if (StringUtils.hasText(password) && !password.isEmpty()) {
            builder.password(password);
        }
        
        return builder.build();
    }
}